package com.wangzhenyu.learn.test.designPattern


//状态模式

@Suppress("UNUSED")
object VStatePattern {

    class WaterMachine {

        var state: WaterMachineState

        val off = Off(this)
        val heating = Heating(this)
        val cooling = Cooling(this)

        init {
            this.state = off
        }

        fun turnHeating() {
            this.state.turnHeating()
        }

        fun turnCooling() {
            this.state.turnCooling()
        }

        fun turnOff() {
            this.state.turnOff()
        }

    }

    sealed class WaterMachineState(private val machine: WaterMachine) {
        fun turnHeating() {
            if (this !is Heating) {
                machine.state = machine.heating
            } else {
                print("已经是该状态了")
            }
        }

        fun turnCooling() {
            if (this !is Cooling) {
                machine.state = machine.cooling
            } else {
                print("已经是该状态了")
            }
        }

        fun turnOff() {
            if (this !is Off) {
                machine.state = machine.off
            } else {
                print("已经是该状态了")
            }
        }

    }

    class Off(machine: WaterMachine) : WaterMachineState(machine)
    class Heating(machine: WaterMachine) : WaterMachineState(machine)
    class Cooling(machine: WaterMachine) : WaterMachineState(machine)


}




